<template>
	<view class="content">
		<view class="dynamics-list">
			<view class="dynamics-item" v-for="(item, i) in list" :key="i">
				<view class="user-wrap">
					<image class="avatar" :src="getAvatarUrl(item.user.avatar)" mode="aspectFill" @click="openUserDetails(item.user)"></image>
					<view class="user-info" @click="openUserDetails(item.user)">
						<view class="name-wrap">
							<text class="name">{{item.user.name}}</text>
							<view :class="['sex-wrap', 'sex' + item.user.sex]">
								<image class="sex-icon" :src="'/static/img/sex' + item.user.sex + '.png'" mode=""></image>
								<text class="age">{{item.user.age}}</text>
							</view>
						</view>
						<view class="time">{{item.created_at | timeFormat}}</view>
					</view>
					<view class="handle-wrap">
						<image v-if="item.is_follow == 0" class="follow-icon" @click="follow(item.user, i)" :src="'/static/img/other'+ item.is_follow +'.png'" mode=""></image>
						<image v-else class="im-icon" @click="openIM(item.user)" :src="'/static/img/other'+ item.is_follow +'.png'" mode=""></image>
					</view>
				</view>
				<view class="dynamics-content">
					<view class="content">
						<text v-if="!!item.description" class="desc" @click="openDynamicsDetail(item)">{{item.description}}</text>
						<text v-if="!!item.topic" class="topic">#{{item.topic.name}}#</text>
					</view>
					<view class="media-wrap">
						<view class="media-wrap-bg" @click="openDynamicsDetail(item)"></view>
						<view v-if="item.type == 1" class="img" v-for="(media, j) in item.files" :key="media.id" @click="previewImage(item.files, j)">
							<image :src="media.file + '?imageView2/1/w/300/h/300'" mode=""></image>
						</view>
						<view v-if="item.type == 2" class="video" v-for="(media, k) in item.files" :key="media.id" @click="openDynamicsVideo(media)">
							<image class="cover-image" :src="media.file + '?vframe/jpg/offset/1'" mode="aspectFill"></image>
							<image class="player-icon" src="/static/img/player_icon.png" mode=""></image>
						</view>
					</view>
				</view>
				<view class="dynamics-menu">
					<view class="menu-item" @click="openDynamicsDetail(item)">
						<image class="menu-icon" src="/static/img/comment.png" mode=""></image>
						<text>{{item.comment_size}}</text>
					</view>
					<view v-if="type != 'my'" class="menu-item" @click="like(item, i)">
						<image class="menu-icon" :src="'/static/img/like'+ item.is_like +'.png'" mode=""></image>
						<text>{{item.like_size}}</text>
					</view>
					<view class="menu-item"></view>
					<view class="menu-item"></view>
				</view>
			</view>
			<view v-if="empty" class="empty-wrap">
				<image class="empty-icon" src="/static/img/empty.png" mode=""></image>
				<text class="empty-text">暂无动态</text>
			</view>
		</view>
		<uni-load-more :status="status" :content-text="contentText"></uni-load-more>
	</view>
</template>

<script>
	import util from "@/common/util.js"
	import uniLoadMore from "@/components/uni-load-more/uni-load-more.vue"
	export default {
		components: {
			uniLoadMore
		},
		data() {
			return {
				type: '',
				topic: [],
				myInfo: {},
				tid: 0,
				title: '',
				empty: false,
				list: [],
				page: 1,
				status: 'more',
				contentText: {
					contentdown: '加载更多',
					contentrefresh: '加载中',
					contentnomore: '木有了~'
				},
			}
		},
		onLoad(options) {
			this.type = options.type;
			if (this.type == 'topic') {
				this.tid = options.tid;
				if (this.tid == 0) {
					this.title = options.title
				} else {
					this.title = '#' + options.title + '#'
				}
			}
			if (this.type == 'my') {
				this.title = '我的动态'
			}
			uni.setNavigationBarTitle({
				title: this.title
			})
			try {
				const myInfo = uni.getStorageSync('myInfo');
				if (myInfo) {
					this.myInfo = myInfo;
				}
			} catch (e) {
				console.log(e)
			}
		},
		onReady() {
			if (this.type == 'topic') {
				this.getDynamic();
			}
			if (this.type == 'my') {
				this.getUserDynamic();
			}
		},
		onPullDownRefresh() {
			this.page = 1;
			setTimeout(() => {
				if (this.type == 'topic') {
					this.getDynamic();
				}
				if (this.type == 'my') {
					this.getUserDynamic();
				}
			}, 500)
		},
		onReachBottom() {
			this.status = 'loading';
			if (this.type == 'topic') {
				this.getDynamic();
			}
			if (this.type == 'my') {
				this.getUserDynamic();
			}
		},
		onNavigationBarButtonTap(e) {
			let list = []
			for (let i = 0; i < this.topic.length; i++) {
				list.push({
					id: this.topic[i].id,
					name: this.topic[i].name
				})
			}
			uni.navigateTo({
				url: '../dynamics/post?topic=' + JSON.stringify(list)
			})
		},
		methods: {
			getDynamic: function () {
				uni.showLoading({
					title: ''
				});
				this.$api.api.ajaxData({
					method: 'V1.Dynamic',
					uid: this.myInfo.id,
					token: this.myInfo.token,
					tid: this.tid,
					page: this.page
				}, res => {
					uni.hideLoading();
					uni.stopPullDownRefresh();
					if (res.status) {
						if (this.page == 1) {
							this.topic = res.data.topic
							this.list = res.data.dynamic
							if (this.list.length == 0) {
								this.empty = true;
							}
							this.page++
							this.status = 'more'
						} else {
							if (res.data.dynamic.length > 0) {
								for (let i = 0; i < res.data.dynamic.length; i++) {
									this.list.push(res.data.dynamic[i])
								}
								this.page++
								this.status = 'more'
							} else {
								this.status = 'noMore'
							}
						}
					} else {
						uni.showToast({
							title: res.msg,
							position: 'bottom',
							icon: 'none'
						});
					}
				}, err => {
					uni.hideLoading();
					uni.showToast({
						title: '网络错误，请稍后再试',
						position: 'bottom',
						icon: 'none'
					});
				})
			},
			
			getUserDynamic: function () {
				uni.showLoading({
					title: ''
				});
				this.$api.api.ajaxData({
					method: 'V1.UserDynamic',
					uid: this.myInfo.id,
					token: this.myInfo.token,
					page: this.page
				}, res => {
					uni.hideLoading();
					uni.stopPullDownRefresh();
					if (res.status) {
						if (this.page == 1) {
							this.topic = res.data.topic
							this.list = res.data.dynamic
							if (this.list.length == 0) {
								this.empty = true;
							} else {
								this.empty = false;
							}
							this.page++
							this.status = 'more'
						} else {
							if (res.data.dynamic.length > 0) {
								for (let i = 0; i < res.data.dynamic.length; i++) {
									this.list.push(res.data.dynamic[i])
								}
								this.page++
								this.status = 'more'
							} else {
								this.status = 'noMore'
							}
						}
					} else {
						uni.showToast({
							title: res.msg,
							position: 'bottom',
							icon: 'none'
						});
					}
				}, err => {
					uni.hideLoading();
					uni.showToast({
						title: '网络错误，请稍后再试',
						position: 'bottom',
						icon: 'none'
					});
				})
			},
			
			openUserDetails: function (item) {
				uni.navigateTo({
					url: '../home/user_home?user=' + JSON.stringify(item)
				})
			},
			
			follow: function (item, index) {
				uni.showLoading({
					title: ''
				});
				this.$api.api.ajaxData({
					method: 'V1.Follow',
					uid: this.myInfo.id,
					token: this.myInfo.token,
					type: 'follow',
					touid: item.id,
				}, res => {
					uni.hideLoading();
					if (res.status) {
						let temp = this.list[index];
						temp.is_follow = 1
						this.$set(this.list, index, temp)
					} else {
						uni.showToast({
							title: res.msg,
							position: 'bottom',
							icon: 'none'
						});
					}
				}, err => {
					uni.hideLoading();
					uni.showToast({
						title: '网络错误，请稍后再试',
						position: 'bottom',
						icon: 'none'
					});
				})
			},
			
			openIM: function (item) {
				let to = {
					conversationID: 'C2C' + this.IDBEFORE + item.id,
					id: this.IDBEFORE + item.id,
					name: item.name,
					avatar: item.avatar
				}
				uni.navigateTo({
					url: '../message/im?to=' + encodeURIComponent(JSON.stringify(to))
				});
			},
			
			openDynamicsDetail: function (item) {
				uni.navigateTo({
					url: 'dynamics_detail?id=' + item.id
				})
			},
			
			like: function (item, index) {
				let type = ''
				if (item.is_like == 0) {
					type = 'like'
				}
				if (item.is_like == 1) {
					type = 'other'
				}
				uni.showLoading({
					title: ''
				});
				this.$api.api.ajaxData({
					method: 'V1.DynamicLike',
					uid: this.myInfo.id,
					token: this.myInfo.token,
					did: item.id,
					type: type,
				}, res => {
					uni.hideLoading();
					if (res.status) {
						if (type == 'like') {
							uni.showToast({
								title: '点赞成功',
								position: 'bottom',
								icon: 'none'
							});
							let temp = this.list[index]
							temp.is_like = 1
							temp.like_size++
							this.$set(this.list, index, temp)
						}
						if (type == 'other') {
							uni.showToast({
								title: '取消点赞',
								position: 'bottom',
								icon: 'none'
							});
							let temp = this.list[index]
							temp.is_like = 0
							temp.like_size--
							this.$set(this.list, index, temp)
						}
					} else {
						uni.showToast({
							title: res.msg,
							position: 'bottom',
							icon: 'none'
						});
					}
				}, err => {
					uni.hideLoading();
					uni.showToast({
						title: '网络错误，请稍后再试',
						position: 'bottom',
						icon: 'none'
					});
				})
			},
			
			previewImage: function (list, index) {
				let templist = [];
				for (let i = 0; i < list.length; i++) {
					templist.push(list[i].file);
				}
				uni.previewImage({
					current: index,
					urls: templist,
			  });
			},
			
			openDynamicsVideo: function (item) {
				let media = {
					file: item.file
				}
				uni.navigateTo({
					url: '../video/video?media=' + encodeURIComponent(JSON.stringify(media))
				})
			},
			
			getAvatarUrl: function (value) {
				return value.indexOf(this.QINIUURL) == -1 ? value : value + '?imageView2/1/w/100/h/100'
			}
		},
		filters: {
			timeFormat: function (value) {
				let now = new Date().getTime();
				let old = value
				let	oldTime = 0
				if (typeof value === 'number') {
					oldTime = old*1000;
				}
				if (typeof value === 'string') {
					oldTime = new Date(value.replace(/-/g, '/')).getTime();
				}
				return util.dateUtils.humanize(now - oldTime);
			}
		}
	}
</script>

<style lang="scss">
	@import '../../common/common.scss';
	.dynamics-item {
		background-color: #fff;
		padding: 20rpx;
		margin-bottom: 10rpx;
		.user-wrap {
			display: flex;
			justify-content: center;
			align-items: center;
			.avatar {
				width: 120rpx;
				height: 120rpx;
				border-radius: 120rpx;
				background-color: $image-bg-color;
			}
			.user-info {
				flex: 1;
				margin: 0 10rpx;
				.name-wrap {
					display: flex;
					justify-content: flex-start;
					align-items: center;
				}
				.name {
					max-width: 360rpx;
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
					font-size: 36rpx;
					color: #333;
					margin-right: 10rpx;
				}
				.sex-wrap {
					display: flex;
					justify-content: center;
					align-items: center;
					line-height: 28rpx;
					padding: 5rpx 10rpx;
					border-radius: 28rpx;
					&.sex1 {
						background: #83d7ff;
					}
					&.sex2 {
						background: #fe9ab7;
					}
					.sex-icon {
						width: 28rpx;
						height: 28rpx;
					}
					.age {
						font-size: 28rpx;
						color: #fff;
						margin-left: 5rpx;
					}
				}
				.time {
					font-size: 30rpx;
					color: #666666;
				}
			}
			.handle-wrap {
				display: flex;
				justify-content: center;
				align-items: center;
				.follow-icon, .im-icon {
					width: 60rpx;
					height: 60rpx;
				}
			}
		}
		.dynamics-content {
			margin-left: 110rpx;
			.content {
				.desc {
					font-size: 32rpx;
					color: #333;
					line-height: 40rpx;
					margin-right: 10rpx;
				}
				.topic {
					font-size: 32rpx;
					color: #007AFF;
					line-height: 40rpx;
				}
			}
			.media-wrap {
				display: flex;
				flex-wrap: wrap;
				position: relative;
				.media-wrap-bg {
					position: absolute;
					left: 0;
					right: 0;
					top: 0;
					bottom: 0;
					z-index: 1;
				}
				.img {
					width: 200rpx;
					height: 200rpx;
					padding: 5rpx;
					box-sizing: border-box;
					position: relative;
					z-index: 2;
					image {
						display: block;
						width: 190rpx;
						height: 190rpx;
						border-radius: 10rpx;
					}
				}
				.video {
					width: 600rpx;
					height: 360rpx;
					padding: 5rpx;
					box-sizing: border-box;
					position: relative;
					z-index: 2;
					.cover-image {
						display: block;
						width: 590rpx;
						height: 350rpx;
						border-radius: 10rpx;
					}
					.player-icon {
						width: 50rpx;
						height: 50rpx;
						position: absolute;
						left: 50%;
						top: 50%;
						margin-left: -25rpx;
						margin-right: -25rpx;
					}
				}
			}
		}
		.dynamics-menu {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-left: 110rpx;
			.menu-item {
				display: flex;
				align-items: center;
				padding: 20rpx 0;
				margin-right: 50rpx;
				.menu-icon {
					width: 40rpx;
					height: 40rpx;
					margin-right: 10rpx;
				}
				text {
					font-size: 36rpx;
					color: #666;
				}
			}
		}
	}
</style>
